﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using ROOT.MICROSOFTIDENTITYINTEGRATIONSERVER;


namespace IdentityNotes.Examples.WMIPasswordResetService
{
    public class WMIPasswordresetService
    {
        public void resetPassword(string newPassword,string MVObjectGuid,string MAName)
        {
            const string MIIS_WMI_Namespace = @"root\MicrosoftIdentityIntegrationServer";

            //Create WMI connection to local server (assumes web service is installed on same machine as ILM Sync
            ConnectionOptions connOpt = new ConnectionOptions();
            connOpt.Authentication = AuthenticationLevel.PacketPrivacy;
            
            ManagementScope WMINamespace;
            WMINamespace = new ManagementScope(MIIS_WMI_Namespace,connOpt);
            
            //Generate WMI query text
            string wmiQueryString = String.Format("Select * from MIIS_CSObject where MVGiud={0}",MVObjectGuid);

            // Run the WMI query.
            string condition = String.Format("MvGuid = '{0}'",MVObjectGuid);
            CSObject.CSObjectCollection csObjects;

            csObjects = CSObject.GetInstances(WMINamespace,condition);

            int passwordChangeCount = 0;

            foreach (CSObject csobject in csObjects)
            {
                if(csobject.MaName.ToUpper() == MAName.ToUpper())
                {
                    csobject.SetPassword(true,newPassword,true, false);
                    passwordChangeCount++;
                }
           
            }

        }
    }
}
